package com.azunitech.jms;

import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.UUID;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.StreamMessage;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.junit.Test;

import com.azunitech.jms.common.EventMessage;
import com.azunitech.jms.common.domains.PartsEntity;

public class EventProcessorMDBTester {
	public final static String JNDI_FACTORY="weblogic.jndi.WLInitialContextFactory";
	public final static String JMS_FACTORY="CF-COMMAND";
	public final static String QUEUE="Queue-001-event";
	public final static String URL = "t3://localhost:7001";
	java.util.Date date= new java.util.Date();
	
	@Test public void sendOneMsg() throws Exception{
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
		env.put(Context.PROVIDER_URL, URL);
		InitialContext ctx =  new InitialContext(env);
		
		QueueConnectionFactory qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
		QueueConnection qcon = qconFactory.createQueueConnection();
		QueueSession qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue = (Queue) ctx.lookup(QUEUE);
		QueueSender qsender = qsession.createSender(queue);
		qcon.start();
		
		for( int i = 0; i < 10; i ++){
			ObjectMessage partMdg = sendEventMessage(qsession, i);
			qsender.send(partMdg);
		}
			
		qsender.close();
		qsession.close();
		qcon.close();
	}
	
	private ObjectMessage sendEventMessage(QueueSession qsession, int i) throws JMSException{
		ObjectMessage objectMessage = qsession.createObjectMessage();
        objectMessage.setObject(new EventMessage( i, UUID.randomUUID().toString() ));
        return objectMessage;
	}
}
